﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Module_Itinerary : PanelControl
{
    protected int flightDi = Helper.getQuery("flightDi");
    protected int classDi = Helper.getQuery("classDi");
    protected int flightVe = Helper.getQuery("flightVe");
    protected int classVe = Helper.getQuery("classVe");
    FlightBusiness fb=new FlightBusiness();
    ClassBusiness cb=new ClassBusiness();
    AirfareTypeBusiness atb=new AirfareTypeBusiness();
    PromotionsBuisness pb=new PromotionsBuisness();
    PlaneBusiness plb=new PlaneBusiness();
    AirfareBusiness ab = new AirfareBusiness();
    
    double tax = Convert.ToDouble("0,1");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (flightDi == 0 && classDi == 0 && flightVe == 0 && classVe == 0)
        {
            BookingEntities be = new BookingEntities();
            be=(BookingEntities)System.Web.HttpContext.Current.Session["datve"];
            flightDi = be._flightDi;
            classDi = be._classDi;
            flightVe = be._flightVe;
            classVe = be._classVe;
        }      
            lblPriceDi.Text = ab.getPrice(this.getFlightDetail(flightDi)._price, this.getBooking()._totalNguoiLon, this.getBooking()._totalTreEm, this.getClass(classDi)._classFactor, this.getPromotions(this.getFlightDetail(flightDi)._promotionsID)._promotionsFactor, tax, this.getBooking()._khuhoi).ToString();
            lblPriceVe.Text = ab.getPrice(this.getFlightDetail(flightVe)._price, this.getBooking()._totalNguoiLon, this.getBooking()._totalTreEm, this.getClass(classVe)._classFactor, this.getPromotions(this.getFlightDetail(flightVe)._promotionsID)._promotionsFactor, tax, this.getBooking()._khuhoi).ToString();
            lblTotalPrice.Text = (Convert.ToDouble(lblPriceDi.Text) + Convert.ToDouble(lblPriceVe.Text)).ToString();

            if (this.getBooking()._khuhoi == "False")
            {
                this.tblChuyenDi.Visible = false;
            }
    }

    
    public PromotionsEntities getPromotions(int flightID)
    {
        return pb.getPromotions(flightID);
    }

    public BookingEntities getBooking()
    {
        return (BookingEntities)System.Web.HttpContext.Current.Session["datve"];
    }
    public FlightEntities getFlightDetail(int flightID)
    {
        return fb.getFlight(flightID);
    }

    public ClassEntities getClass(int classID)
    {
        return cb.getClass(classID);
    }

    public AirfareTypeEntities getAirfareType(int airfareTypeID)
    {
        return atb.getAirfareType(airfareTypeID);
    }

    public PlaneEntities getPlane(int planeID)
    {
        return plb.getPlane(planeID);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session["datve"] = null;
        Response.Redirect("Default.aspx?cid=2");
    }
    protected void btnMua_Click(object sender, EventArgs e)
    {
        if (Helper.logIn())
        {
            UserEntities ue = (UserEntities)Session["user"];
            for (int i = 1; i <= this.getBooking()._totalNguoiLon; i++)
            {
                string[] values = { flightDi.ToString(), ue._userID.ToString(), classDi.ToString(), "1", ab.getPrice(this.getFlightDetail(flightDi)._price, i, 0, this.getClass(classDi)._classFactor, this.getPromotions(this.getFlightDetail(flightDi)._promotionsID)._promotionsFactor, tax, this.getBooking()._khuhoi).ToString() };
                ab.Insert(values);
            }
            if (this.getBooking()._totalTreEm > 0)
            {
                for (int j = 1; j <= this.getBooking()._totalTreEm; j++)
                {
                    string[] values = { flightDi.ToString(), ue._userID.ToString(), classDi.ToString(), "2", ab.getPrice(this.getFlightDetail(flightDi)._price, 0, j, this.getClass(classDi)._classFactor, this.getPromotions(this.getFlightDetail(flightDi)._promotionsID)._promotionsFactor, tax, this.getBooking()._khuhoi).ToString() };
                    ab.Insert(values);
                }
            }
            if (this.getBooking()._khuhoi == "True")
            {
                for (int i = 1; i <= this.getBooking()._totalNguoiLon; i++)
                {
                    string[] values = { flightVe.ToString(), ue._userID.ToString(), classVe.ToString(), "1", ab.getPrice(this.getFlightDetail(flightVe)._price, i, 0, this.getClass(classVe)._classFactor, this.getPromotions(this.getFlightDetail(flightVe)._promotionsID)._promotionsFactor, tax, this.getBooking()._khuhoi).ToString() };
                    ab.Insert(values);
                }

                if (this.getBooking()._totalTreEm > 0)
                {
                    for (int j = 1; j <= this.getBooking()._totalTreEm; j++)
                    {
                        string[] values = { flightVe.ToString(), ue._userID.ToString(), classVe.ToString(), "2", ab.getPrice(this.getFlightDetail(flightVe)._price, 0, j, this.getClass(classVe)._classFactor, this.getPromotions(this.getFlightDetail(flightVe)._promotionsID)._promotionsFactor, tax, this.getBooking()._khuhoi).ToString() };
                        ab.Insert(values);
                    }
                }
            }
            Response.Redirect("Default.aspx?url=Module/Profile.ascx&userID=" + ue._userID);
        }
        else
        {
            BookingEntities be = new BookingEntities();
            be = (BookingEntities)System.Web.HttpContext.Current.Session["datve"];
            be._flightDi = flightDi;
            be._classDi= classDi;
            be._flightVe = flightVe;
            be._classVe = classVe;
            Session["datve"] = be;
            Label1.Text = "Hãy đăng nhập để hoàn tất việc đăng ký";
        }
    }
}